<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Expense_c extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper(array('url', 'html', 'file'));
        $this->load->model('expense_m');
    }

    function index() {
        $data['exp_data'] = $this->expense_model->get_expenses();
        $data['voucher_data'] = $this->expense_model->get_voucher();
        $data['content'] = 'pages/expense';
        $data['title'] = '';
        $data['current_page'] = '';
        $data['back_to'] = 'none';
        $this->load->view('template', $data);
    }
    function get_expenses($event_id){
        echo json_encode(array('aaData'=>$this->expense_m->get_expenses($event_id)));
    }
    function get_voucher($event_id){
        echo json_encode(array('aaData'=>$this->expense_m->get_voucher($event_id)));
    }
    function save_expense() {
        $event_id = $this->input->post('event_id');
        $exp_item = $this->input->post('exp_item');
        $particulars = $this->input->post('particulars');
        $estimated_amount = $this->input->post('estimated_amount');
        $actual_amount = $this->input->post('actual_amount');
        $receipt_num = $this->input->post('receipt_num');
        $purchase_date = $this->input->post('purchase_date');
        if (isset($_POST['exp_cheque'])) {
            $exp_cheque = 1;
            $exp_chq_no = $this->input->post('exp_chq_no');
            $remarks = $this->input->post('remarks');
        } else {
            $exp_cheque = 0;
            $remarks = $this->input->post('remarks');
        }
        
        if ($exp_item == NULL) {
            echo 'The fields are null';
        } else {
            $data1 = array(
                'exp_item' => $exp_item,
                'particulars' => $particulars,
                'estimated_amount' => $estimated_amount,
                'actual_amount' => $actual_amount,
                'receipt_num' => $receipt_num,
                'exp_cheque' => $exp_cheque,
                'exp_chq_no' => $exp_chq_no,
                'purchase_date' => $purchase_date,
                'remarks' => $remarks,
                'event_id' => $event_id,
            );
            $this->expense_m->insert_expense($data1);
        }
        echo json_encode('success');
    }

    function save_voucher() {
        $event_id = $this->input->post('event_id');
        $cv_num = $this->input->post('cv_num');
        $cv_desc = $this->input->post('cv_desc');
        $cv_amount = $this->input->post('cv_amount');
        if (isset($_POST['cv_cheque'])) {
            $cv_cheque = 1;
            $cv_remarks = $this->input->post('cv_remarks');
            $cv_chq_no = $this->input->post('cv_chq_no');
        } else {
            $cv_cheque = 0;
            $cv_remarks = $this->input->post('cv_remarks');
        }
        if ($_POST['cv_status'] === "Paid") {
            $cv_status = 1;
        } else {
            $cv_status = 0;
        }

        if ($cv_desc == NULL) {
            echo 'The fields are null';
        } else {
            $data1 = array(
                'cv_num' => $cv_num,
                'cv_desc' => $cv_desc,
                'cv_amount' => $cv_amount,
                'cv_cheque' => $cv_cheque,
                'cv_chq_no' => $cv_chq_no,
                'cv_remarks' => $cv_remarks,
                'cv_status' => $cv_status,
                'event_id' => $event_id,
            );
            $this->expense_m->insert_voucher($data1);
        }
        echo json_encode('success');
    }

    function edit_expense() {
        $data['exp_data_entry'] = $this->expense_model->get_exp_entry($this->input->post('exp_entry'));
        foreach ($data['exp_data_entry']->result() as $row) {
            $data1 = array(
                'exp_item' => $row->exp_item,
                'particulars' => $row->particulars,
                'estimated_amount' => $row->estimated_amount,
                'actual_amount' => $row->actual_amount,
                'receipt_num' => $row->receipt_num,
                'exp_cheque' => $row->exp_cheque,
                'exp_chq_no' => $row->exp_chq_no,
                'purchase_date' => $row->purchase_date,
                'remarks' => $row->remarks,
            );
        }
        echo json_encode($data1);
    }

    function edit_voucher() {
        $data['cv_data_entry'] = $this->expense_model->get_cv_entry($this->input->post('cv_entry'));
        foreach ($data['cv_data_entry']->result() as $row) {
            $data1 = array(
                'cv_num' => $row->cv_num,
                'cv_desc' => $row->cv_desc,
                'cv_amount' => $row->cv_amount,
                'cv_cheque' => $row->cv_cheque,
                'cv_chq_no' => $row->cv_chq_no,
                'cv_remarks' => $row->cv_remarks,
                'cv_status' => $row->cv_status,
            );
        }
        echo json_encode($data1);
    }
    
    function budget_balance($event_id){
        $query = $this->expense_m->compute_balance($event_id);
        $num = number_format($query->balance, 2, '.', ',');
        $data = 'success';
        echo json_encode (array('data'=> $data, 'result'=>$num)); 
    }
    
    function event_initial_budget($event_id){
        $query = $this->expense_m->get_event_initial_budget($event_id);
        $num = number_format($query->initial_budget, 2, '.', ',');
        $data = 'success';
        echo json_encode (array('data'=> $data, 'result'=>$num)); 
    }
    
    function expense_actual_amount($event_id){
        $query = $this->expense_m->get_exp_actual_amount($event_id);
        $num = number_format($query->exp_total, 2, '.', ',');
        $data = 'success';
        echo json_encode (array('data'=> $data, 'result'=>$num)); 
    }
    
    function voucher_amount($event_id){
        $query = $this->expense_m->get_cv_amount($event_id);
        $num = number_format($query->total, 2, '.', ',');
        $data = 'success';
        echo json_encode (array('data'=> $data, 'result'=>$num)); 
    }
    
    function expense_export_table($event_id){
        $query = $this->expense_m->get_table_expense($event_id);
        $res = "";
        $result = "";
        foreach ($query->result() as $row){
            $n ="<br />";
            $item = $row->exp_item;
            $parti = $row->particulars;
            $eamount = $row->estimated_amount;
            $aamount = $row->actual_amount;
            $rnum = $row->receipt_num;
            $date = $row->purchase_date;
            if($row->exp_cheque === "1"){
                $cheque = $row->exp_chq_no;
            } else {
                $cheque = "";
            }
            $remarks = $row->remarks;

            $res = $res .'<tr>
                            <td style="border:black solid 2px;">'.$item. '</td>
                            <td style="border:black solid 2px;">' .$parti. '</td>
                            <td style="border:black solid 2px;">' .$eamount. '</td>
                            <td style="border:black solid 2px;">' .$aamount. '</td>
                            <td style="border:black solid 2px;">' .$rnum.'</td>
                            <td style="border:black solid 2px;">' .$date. '</td>
                            <td style="border:black solid 2px;">' .$cheque. '</td>
                            <td style="border:black solid 2px;">' .$remarks.'</td>
                        </tr>';
            }
             $result = '<table>
                        <thead>
                        <tr>  
                            <th style="border:black solid 2px;">Item</th>
                            <th style="border:black solid 2px;">Particulars</th>
                            <th style="border:black solid 2px;">Estimated Amount</th>
                            <th style="border:black solid 2px;">Actual Amount</th>
                            <th style="border:black solid 2px;">Receipt No.</th>
                            <th style="border:black solid 2px;">Date</th>
                            <th style="border:black solid 2px;">Cheque</th>
                            <th style="border:black solid 2px;">Remarks</th>
                        </tr> 
                        </thead>'
                        .$res.
                        '</table>';

            $data = 'success';
            echo json_encode (array('data'=> $data, 'result'=>$result));  
    }
        
    function voucher_export_table($event_id){
        $query = $this->expense_m->get_table_voucher($event_id);
        $res = "";
        $result = "";
        foreach ($query->result() as $row){
            $n ="<br />";
            $cvnum = $row->cv_num;
            $cvdesc = $row->cv_desc;
            $cvamount = $row->cv_amount;
            if($row->cv_cheque === "1"){
                $cheque = $row->cv_chq_no;
            } else {
                $cheque = "";
            }
            $remarks = $row->cv_remarks;
            if($row->cv_status === "1"){
                $status = "Paid";
            } else {
                $status = "Not Paid";
            }

            $res = $res .'<tr>
                            <td style="border:black solid 2px;">'.$cvnum. '</td>
                            <td style="border:black solid 2px;">' .$cvdesc. '</td>
                            <td style="border:black solid 2px;">' .$cvamount. '</td>
                            <td style="border:black solid 2px;">' .$cheque. '</td>
                            <td style="border:black solid 2px;">' .$remarks.'</td>
                            <td style="border:black solid 2px;">' .$status. '</td>
                        </tr>';
            }
             $result = '<table>
                        <thead>
                        <tr>  
                            <th style="border:black solid 2px;">CV No.</th>
                            <th style="border:black solid 2px;">Description</th>
                            <th style="border:black solid 2px;">Amount</th>
                            <th style="border:black solid 2px;">Cheque</th>
                            <th style="border:black solid 2px;">Remarks</th>
                            <th style="border:black solid 2px;">Status</th>
                        </tr> 
                        </thead>'
                        .$res.
                        '</table>';

            $data = 'success';
            echo json_encode (array('data'=> $data, 'result'=>$result));  
    }
}
